package com.backedserver.controller.judicialcheck;

import com.common.base.BindCreateBy;
import com.common.base.BindUpdateBy;
import com.common.base.DictionaryAware;
import com.common.global.BizException;
import com.common.global.BizServiceEnum;
import com.common.requestvo.casemanage.CaseForQualityReqVo;
import com.common.requestvo.casemanage.CaseQualityFlowReqVo;
import com.common.service.casemanage.CaseService;
import com.common.service.system.UserService;
import com.common.utils.*;
import com.common.vo.PageVo;
import com.common.vo.ResultVo;
import com.common.vo.casemanage.CaseForQualityFlowVo;
import com.common.vo.casemanage.CaseForRiskVo;
import com.common.vo.casemanage.CaseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 指标导入Controller
 *
 * @author johnny
 * @create 2021-05-15 8:17 上午
 **/

@Api(tags = "司法业绩考核-办案质量模块")
@RestController
@RequestMapping("/judicial_case_check")
@Slf4j
public class JudicialCaseCheckController {


    private final CaseService caseService;


    private final UserService userService;


    public JudicialCaseCheckController(CaseService caseService, UserService userService) {
        this.caseService = caseService;
        this.userService = userService;
    }


    @GetMapping("/listByCondition")
    @ApiOperation(value = "根据条件查询  办结案件 信息 (分页) [适用于 办案质量中 3个页面（风险评估，质量监控，流程监控）的页面查询]")
    public ResultVo listByCondition(CaseForQualityReqVo caseForQualityReqVo,
                                    PageVo pageVo,
                                    HttpServletRequest request) {

        pageVo.setPageNumber(pageVo.getPageNumber() - 1);
        List<String> userRolesFromToken = JwtTokenUtils.getUserRolesFromToken(TokenUtils.getTokenFromRequest(request));
        //判断是否 有查询全部的权限
        if (CollectionUtils.isEmpty(CollectionUtils.intersection(DictionaryAware.ALL_RISK_QUALITY_FLOW_FORM_ROLES, userRolesFromToken))) {
            Long userId = JwtTokenUtils.getUserIdFromToken(TokenUtils.getTokenFromRequest(request));
            Long personalId = userService.findPersonalIdByUserId(userId);
            if (personalId == null) {
                throw new BizException(BizServiceEnum.FIND_NOT_CURRENT_USER_PERSONAL_INFO);
            }
            //如果没有 特定权限 则只能查看当前登录人的
            caseForQualityReqVo.setPersonalId(personalId);
        }
        Page<CaseVo> page = caseService.listByConditionForQuality(caseForQualityReqVo, PageUtil.initPage(pageVo));
        return ResultVoUtil.success(page);
    }

    @GetMapping("/findRiskForCaseByCaseId/{id}")
    @ApiOperation(value = "根据caseId 查询 风险评估 只有1个")
    public ResultVo findRiskForCaseByCaseId(@PathVariable("id") Long caseId) {
        CaseForRiskVo riskForCaseByCaseId = caseService.findRiskForCaseByCaseId(caseId);
        return ResultVoUtil.success(riskForCaseByCaseId);
    }

    @PostMapping("/addRiskForCase")
    @ApiOperation(value = "添加 风险评估")
    @BindCreateBy
    public ResultVo addRiskForCase(@RequestBody CaseForRiskVo caseForRiskVo) {
        caseService.addRiskForCase(caseForRiskVo);
        return ResultVoUtil.success();
    }


    @PostMapping("/editRiskForCase")
    @ApiOperation(value = "编辑 风险评估")
    @BindUpdateBy
    public ResultVo editRiskForCase(@RequestBody CaseForRiskVo caseForRiskVo) {
        caseService.editRiskForCase(caseForRiskVo);
        return ResultVoUtil.success();
    }

    @GetMapping("/deleteRiskForCase/{id}")
    @ApiOperation(value = "根据id删除 风险评估报告")
    public ResultVo deleteRiskForCase(@PathVariable Long id) {
        caseService.deleteRiskForCase(id);
        return ResultVoUtil.success();
    }


    @PostMapping("/addQualityFlowForCase")
    @ApiOperation(value = "添加 质量/流程 监控报告")
    @BindCreateBy
    public ResultVo addQualityFlowForCase(@RequestBody CaseForQualityFlowVo caseForQualityFlowVo) {
        caseService.addQualityFlowForCase(caseForQualityFlowVo);
        return ResultVoUtil.success();
    }

    @PostMapping("/editQualityFlowForCase")
    @ApiOperation(value = "编辑 质量/流程 监控报告")
    @BindUpdateBy
    public ResultVo editQualityFlowForCase(@RequestBody CaseForQualityFlowVo caseForQualityFlowVo) {
        caseService.editQualityFlowForCase(caseForQualityFlowVo);
        return ResultVoUtil.success();
    }

    @GetMapping("/deleteCaseQualityFlowList/{id}")
    @ApiOperation(value = "根据id删除 质量/流程 监控报告")
    public ResultVo deleteCaseQualityFlowList(@PathVariable Long id) {
        caseService.deleteCaseQualityFlowList(id);
        return ResultVoUtil.success();
    }


    @GetMapping("/findCaseQualityFlowList")
    @ApiOperation(value = "根据 caseId 查询 质量/流程 监控报告List")
    public ResultVo findCaseQualityList(CaseQualityFlowReqVo caseQualityFlowReqVo) {
        List<CaseForQualityFlowVo> caseForQualityFlowVos = caseService.findCaseQualityFlowList(caseQualityFlowReqVo);
        return ResultVoUtil.success(caseForQualityFlowVos);
    }


}